package com.ikun.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.ikun.subject.application.convert.SubjectAnswerDTOConverter;
import com.ikun.subject.application.convert.SubjectCategoryDTOConverter;
import com.ikun.subject.application.convert.SubjectInfoDTOConverter;
import com.ikun.subject.application.dto.SubjectAnswerDTO;
import com.ikun.subject.application.dto.SubjectCategoryDTO;
import com.ikun.subject.application.dto.SubjectInfoDTO;
import com.ikun.subject.common.entity.PageResult;
import com.ikun.subject.common.entity.Result;
import com.ikun.subject.domain.entity.SubjectAnswerBO;
import com.ikun.subject.domain.entity.SubjectCategoryBO;
import com.ikun.subject.domain.entity.SubjectInfoBO;
import com.ikun.subject.domain.service.impl.SubjectInfoDomainServiceImpl;
import com.ikun.subject.infra.basic.entity.SubjectCategory;
import com.ikun.subject.infra.basic.entity.SubjectInfo;
import com.ikun.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 刷题controller
 * @Param:
 * @Author: Must.
 * @Date: 2024/4/24 10:42
 */
@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainServiceImpl subjectInfoDomainService;

    /**
     * @Description: 新增题目
     * @Author: gqh
     * @Date: 2024/5/2 4:18
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoDTO.add.subjectInfoDTO:{}", JSON.toJSONString(subjectInfoDTO));
            }
            //参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryIds(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelIds(), "标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            //将 subjectInfo 的 optionList 转换为 BO 类型的
            //注意：是用 SubjectInfoDTOConverter 转换器
            List<SubjectAnswerBO> subjectAnswerBOS =
                    SubjectInfoDTOConverter.INSTANCE.convertListDTOToBO(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOS);
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增题目失败");

        }

    }

    /**
     * @Description: 查询题目列表
     * @Author: gqh
     * @Date: 2024/5/5 15:08
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoDTO.getSubjectPage.subjectInfoDTO:{}", JSON.toJSONString(subjectInfoDTO));
            }
            //参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            PageResult<SubjectInfoBO> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("分页查询题目失败");

        }

    }

    /**
     * @Description: 查询题目信息
     * @Author: gqh
     * @Date: 2024/5/5 19:05
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoDTO.querySubjectInfo.subjectInfoDTO:{}", JSON.toJSONString(subjectInfoDTO));
            }
            //参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
//            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
//            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            SubjectInfoBO boResult = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO dto = SubjectInfoDTOConverter.INSTANCE.convertBOToDTO(boResult);
            return Result.ok(dto);
        } catch (Exception e) {
            log.error("SubjectCategoryController.querySubjectInfo.error:{}", e.getMessage(), e);
            return Result.fail("查询题目详情失败");

        }

    }
}
